package com.bay1ts.service.impl;

import com.bay1ts.dao.EntrepreneurshipMapper;
import com.bay1ts.dao.RequestMapper;
import com.bay1ts.dao.SubjectContestMapper;
import com.bay1ts.dao.VocationaQualificationMapper;
import com.bay1ts.domain.Entrepreneurship;
import com.bay1ts.domain.Request;
import com.bay1ts.domain.SubjectContest;
import com.bay1ts.domain.VocationaQualification;
import com.bay1ts.service.RequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenuu on 2016/4/4.
 */
@Service("requestService")
public class RequestServiceImpl implements RequestService {
    @Autowired
    private SubjectContestMapper subjectContestMapper;
    @Autowired
    private VocationaQualificationMapper vocationaQualificationMapper;
    @Autowired
    private EntrepreneurshipMapper entrepreneurshipMapper;
    @Autowired
    private RequestMapper requestMapper;
    public Map<String, List> getApplyDetailsSortByType(List<Request> list) {
        List<SubjectContest> list1=new ArrayList<SubjectContest>();
        List<VocationaQualification> list2=new ArrayList<VocationaQualification>();
        List<Entrepreneurship> list3=new ArrayList<Entrepreneurship>();
        for (Request request:list){
            if(request.getSubId()!=null){
                //这项记录是学科竞赛的逻辑
                list1.add(subjectContestMapper.selectByPrimaryKey(request.getSubId()));
            }else if (request.getVocId()!=null){
                //这项是职业资格逻辑
                list2.add(vocationaQualificationMapper.selectByPrimaryKey(request.getVocId()));
            }else {
                //这个是创新创业的逻辑.
                list3.add(entrepreneurshipMapper.selectByPrimaryKey(request.getEntId()));
            }
        }
        //已将所有数据得到，并分类。可以放在map里弄出去
        Map<String,List> map=new HashMap<String, List>();
        map.put("subjectcontest",list1);
        map.put("vocationqualification",list2);
        map.put("entrepreneurship",list3);
        return map;
    }

    public int rejectRequest(Map<Long,String> map) {
        for(Map.Entry<Long,String> entry:map.entrySet()){
            Request request=requestMapper.selectByPrimaryKey(entry.getKey());
            //3表示失败，详情参见util里的enum
            request.setStatus(3);
            request.setComment(entry.getValue());
            requestMapper.updateByPrimaryKey(request);
        }
        return map.size();
    }
    //可以重构一下，不单单传入id,也要传入 处理的状态。
    public int firstPassRequest(List<Long> list) {
        for(Long id:list){
            Request request=requestMapper.selectByPrimaryKey(id);
            //通过该项请求(初步通过)
            request.setStatus(1);
            requestMapper.updateByPrimaryKey(request);
        }
        return list.size();
    }

    public int finalPassRequest(List<Long> list) {
        for(Long id:list){
            Request request=requestMapper.selectByPrimaryKey(id);
            //通过该项请求(最终通过)
            request.setStatus(2);
            requestMapper.updateByPrimaryKey(request);
        }
        return list.size();
    }
}
